package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.NoticeListBean;
import com.cljz.makepassenger.ui.holder.SystemMsgListHolder;

import java.util.ArrayList;


/**
 * Created by apple on 2017/5/14.
 */

public class SystemMsgListAdapter extends RecyclerView.Adapter<SystemMsgListHolder> {
    private Context context;
    private ArrayList<NoticeListBean.DataBeanX> noticeList;

    private boolean isDelete;
    private OnItemClickListener mOnItemClickListener;

    public void setDeleteAndData(boolean delete, ArrayList<NoticeListBean.DataBeanX> noticeList) {
        isDelete = delete;
        this.noticeList = noticeList;
        notifyDataSetChanged();
    }


    public void setData(ArrayList<NoticeListBean.DataBeanX> noticeList) {
        this.noticeList = noticeList;
        notifyDataSetChanged();
    }

    public void addData(ArrayList<NoticeListBean.DataBeanX> noticeList) {
        int startPosition = this.noticeList.size() + 1;
        this.noticeList.addAll(noticeList);
        notifyItemRangeInserted(startPosition, noticeList.size());
    }


    public SystemMsgListAdapter(Context context, ArrayList<NoticeListBean.DataBeanX> noticeList, boolean isDelete) {
        this.context = context;
        this.noticeList = noticeList;
        this.isDelete = isDelete;
    }

    @Override
    public SystemMsgListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SystemMsgListHolder(View.inflate(context, R.layout.item_system_msg, null));
    }

    @Override
    public void onBindViewHolder(final SystemMsgListHolder holder, final int position) {
        holder.setData(noticeList, position, isDelete);
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isChecked = ((CheckBox) holder.itemView.findViewById(R.id.cb_selected)).isChecked();
                    mOnItemClickListener.onClick(noticeList, position, isChecked);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return noticeList == null ? 0 : noticeList.size();
    }


    public interface OnItemClickListener {
        void onClick(ArrayList<NoticeListBean.DataBeanX> noticeList, int position, boolean isChecked);

        void onLongClick(ArrayList<NoticeListBean.DataBeanX> noticeList, int position, boolean isChecked);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
